<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        :root {
            --primary: #00f7ff;
            --secondary: #0066ff;
            --dark: #0a0e17;
            --light: #e0f7fa;
            --glow: 0 0 10px rgba(0, 247, 255, 0.7);
        }

        body {
            font-family: 'Microsoft YaHei', 'Segoe UI', sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
            color: var(--light);
            background-color: var(--dark);
            background-image:
                radial-gradient(circle at 10% 20%, rgba(0, 103, 255, 0.1) 0%, transparent 20%),
                radial-gradient(circle at 90% 80%, rgba(0, 247, 255, 0.1) 0%, transparent 20%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .register-container {
            background: linear-gradient(145deg, rgba(16, 22, 36, 0.8) 0%, rgba(0, 40, 80, 0.6) 100%);
            border-radius: 16px;
            padding: 40px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
            border: 1px solid rgba(0, 247, 255, 0.1);
            width: 100%;
            max-width: 400px;
            position: relative;
        }

        .register-container::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 4px;
            background: linear-gradient(to right, var(--primary), var(--secondary));
        }

        h1 {
            text-align: center;
            margin-bottom: 30px;
            font-size: 2em;
            background: linear-gradient(to right, var(--primary), var(--secondary));
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: var(--primary);
            font-weight: bold;
        }

        input {
            width: 100%;
            padding: 12px;
            border: 1px solid rgba(0, 247, 255, 0.3);
            border-radius: 8px;
            background: rgba(10, 14, 23, 0.5);
            color: var(--light);
            font-size: 16px;
            box-sizing: border-box;
            transition: all 0.3s ease;
        }

        input:focus {
            outline: none;
            border-color: var(--primary);
            box-shadow: 0 0 10px rgba(0, 247, 255, 0.3);
        }

        .btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(to right, var(--primary), var(--secondary));
            color: var(--dark);
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 15px;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0, 247, 255, 0.4);
        }

        .btn-secondary {
            background: transparent;
            border: 1px solid var(--primary);
            color: var(--primary);
        }

        .btn-secondary:hover {
            background: var(--primary);
            color: var(--dark);
        }

        .error {
            color: #ff6b6b;
            font-size: 14px;
            margin-top: 5px;
        }

        .success {
            color: #51cf66;
            font-size: 14px;
            margin-top: 5px;
        }

        .tips {
            background: rgba(0, 247, 255, 0.1);
            border: 1px solid rgba(0, 247, 255, 0.3);
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 20px;
            font-size: 14px;
        }

        .tips h4 {
            margin: 0 0 10px 0;
            color: var(--primary);
        }

        .tips ul {
            margin: 0;
            padding-left: 20px;
        }

        .tips li {
            margin-bottom: 5px;
        }
    </style>
</head>

<body>
    <div class="register-container">
        <h1>用户注册</h1>

        <div class="tips">
            <h4>注册须知：</h4>
            <ul>
                <li>用户名必须是11位手机号</li>
                <li>密码至少6位字符</li>
                <li>注册后请联系管理员开通权限</li>
            </ul>
        </div>

        <form id="registerForm">
            <div class="form-group">
                <label for="username">手机号</label>
                <input type="text" id="username" name="username" placeholder="请输入11位手机号" required>
                <div class="error" id="usernameError"></div>
            </div>

            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" placeholder="请输入至少6位密码" required>
                <div class="error" id="passwordError"></div>
            </div>

            <div class="form-group">
                <label for="confirmPassword">确认密码</label>
                <input type="password" id="confirmPassword" name="confirmPassword" placeholder="请再次输入密码" required>
                <div class="error" id="confirmPasswordError"></div>
            </div>

            <button type="submit" class="btn">注册</button>
            <button type="button" class="btn btn-secondary"
                onclick="window.location.href='login.html'">已有账户？去登录</button>
        </form>

        <div class="success" id="successMessage"></div>
    </div>

    <script>
        document.getElementById('registerForm').addEventListener('submit', async function (e) {
            e.preventDefault();

            // 清除之前的错误信息
            clearErrors();

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            // 验证手机号格式
            if (!/^1[3-9]\d{9}$/.test(username)) {
                showError('usernameError', '请输入正确的11位手机号');
                return;
            }

            // 验证密码长度
            if (password.length < 6) {
                showError('passwordError', '密码至少需要6位字符');
                return;
            }

            // 验证密码确认
            if (password !== confirmPassword) {
                showError('confirmPasswordError', '两次输入的密码不一致');
                return;
            }

            try {
                const response = await fetch('register', {
                    method: 'POST',
                    credentials: 'include',  // 添加这行
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ username, password })
                });

                const data = await response.json();
                if (data.code == 1) {
                    document.getElementById('successMessage').textContent = '注册成功！正在跳转到登录页面...';
                    setTimeout(() => {
                        window.location.href = 'login.html';
                    }, 2000);
                } else {
                    showError('usernameError', data.error);
                }

            } catch (error) {
                showError('usernameError', '注册失败，请稍后重试');
            }
        });

        function showError(elementId, message) {
            document.getElementById(elementId).textContent = message;
        }

        function clearErrors() {
            document.getElementById('usernameError').textContent = '';
            document.getElementById('passwordError').textContent = '';
            document.getElementById('confirmPasswordError').textContent = '';
            document.getElementById('successMessage').textContent = '';
        }
    </script>
</body>

</html>